package cn.ffcs.uec.wBStaffInfo.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.model.UecNationduty;
import cn.ffcs.uec.wBStaffInfo.action.bean.WbNationdutyEditBean;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class WbNationdutyEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private WbNationdutyEditBean bean = new WbNationdutyEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 添加职业资格管理
	 */
	private UecNationduty uecNationduty;

	/**
	 * 是否由入职页面引用
	 */
	private boolean isOutStaffEntryExt = false;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$wbNationdutyEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
		this.checkWorknameIsNo();
	}

	/**
	 * 编辑、查看时，要判断职业资格是否为无
	 */
	public void checkWorknameIsNo() {
		String index = (String) bean.getWorkname().getSelectedItem().getValue();
		if ("1001A1100000000006LV".equals(index)) {
			bean.getOccupation().setDisabled(true);
			bean.getWorklevel().setDisabled(true);
			bean.getWorkunitname().setDisabled(true);
			bean.getWorkdate().setDisabled(true);
			bean.getGlbdef4().setDisabled(true);
			bean.getIstop().setDisabled(true);
			//bean.getGlbdef5().setDisabled(true);
			bean.getCertificatenum().setDisabled(true);
			bean.getOccuptype().setDisabled(true);
			// 清楚数据
			bean.getOccupation().setSelectedIndex(0);
			bean.getWorklevel().setSelectedIndex(0);
			bean.getWorkunitname().setValue(null);
			bean.getWorkdate().setValue(null);
			bean.getGlbdef4().setValue(null);
			bean.getIstop().setSelectedIndex(0);
			//bean.getGlbdef5().setSelectedIndex(0);
			bean.getCertificatenum().setValue(null);
			bean.getOccuptype().setValue(null);
		} 
	}
	
	/**
	 * 监听职业资格下拉选择事件
	 */
	public void onSelect$workname() {
		String index = (String) bean.getWorkname().getSelectedItem().getValue();
		if ("1001A1100000000006LV".equals(index)) {
			bean.getOccupation().setDisabled(true);
			bean.getWorklevel().setDisabled(true);
			bean.getWorkunitname().setDisabled(true);
			bean.getWorkdate().setDisabled(true);
			bean.getGlbdef4().setDisabled(true);
			bean.getIstop().setDisabled(true);
			//bean.getGlbdef5().setDisabled(true);
			bean.getCertificatenum().setDisabled(true);
			//bean.getOccuptype().setDisabled(true);
			// 清楚数据
			bean.getOccupation().setSelectedIndex(0);
			bean.getWorklevel().setSelectedIndex(0);
			bean.getWorkunitname().setValue(null);
			bean.getWorkdate().setValue(null);
			bean.getGlbdef4().setValue(null);
			bean.getIstop().setSelectedIndex(0);
			//bean.getGlbdef5().setSelectedIndex(0);
			bean.getCertificatenum().setValue(null);
			//bean.getOccuptype().setValue(null);
		} else {
			bean.getOccupation().setDisabled(false);
			bean.getWorklevel().setDisabled(false);
			bean.getWorkunitname().setDisabled(false);
			bean.getWorkdate().setDisabled(false);
			bean.getGlbdef4().setDisabled(false);
			bean.getIstop().setDisabled(false);
		//	bean.getGlbdef5().setDisabled(false);
			bean.getCertificatenum().setDisabled(false);
		//	bean.getOccuptype().setDisabled(false);
		}
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// 最高istop
		List<NodeVo> nodeVoIstopList = new ArrayList<NodeVo>();
		String[][] istop = { { "Y", "是" }, { "N", "否" } };
		for (int i = 0; i < istop.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(istop[i][j]);
				nodeVo.setName(istop[i][j + 1]);
			}
			nodeVoIstopList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getIstop(), nodeVoIstopList);
		// 岗位职业资格匹配度
	//	ListboxUtils.rendererForEdit(bean.getGlbdef5(), nodeVoIstopList);
		// 职业资格等级worklevel
		ListboxUtils.rendererForEdit(bean.getWorklevel(), NodeVoUtil.getDictionaries(UecConstants.WORK_LEVEL));
		// 职业资格名称workname
		ListboxUtils.rendererForEdit(bean.getWorkname(), NodeVoUtil.getDictionaries(UecConstants.WORK_NAME));
		// 职业资格类别occupation
		ListboxUtils.rendererForEdit(bean.getOccupation(), NodeVoUtil.getDictionaries(UecConstants.WORK_OCCUPATION));

	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		// 初始化时，先填写--职业资格名称--workname
		bean.getOccupation().setDisabled(true);
		bean.getWorklevel().setDisabled(true);
		bean.getWorkunitname().setDisabled(true);
		bean.getWorkdate().setDisabled(true);
		bean.getGlbdef4().setDisabled(true);
		bean.getIstop().setDisabled(true);
		//bean.getGlbdef5().setDisabled(true);
		bean.getCertificatenum().setDisabled(true);
		//bean.getOccuptype().setDisabled(true);

		//isOutStaffEntryExt = (Boolean) arg.get("isOutStaffEntryExt");
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getWbNationdutyEditWindow().setTitle("新增职业资格管理");
			bean.getUecNationdutyEditDiv().setHeight("30%");
			uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getWbNationdutyEditWindow().setTitle("查看职业资格管理");
			uecNationduty = (UecNationduty) arg.get("uecNationduty");
			if (null != uecNationduty) {
				PubUtil.fillBeanFromPo(uecNationduty, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getWbNationdutyEditWindow(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getWbNationdutyEditWindow().setTitle("编辑职业资格管理");
			bean.getUecNationdutyEditDiv().setHeight("30%");
			uecNationduty = (UecNationduty) arg.get("uecNationduty");
			if (null != uecNationduty) {
				PubUtil.fillBeanFromPo(uecNationduty, bean);
				stringTransToDate();
			}
		}
		if ("approvalEdit".equals(opType)) {
			this.bean.getWbNationdutyEditWindow().setTitle("审批职业资格管理信息修改");
			bean.getUecNationdutyEditDiv().setHeight("30%");
			uecNationduty = (UecNationduty) arg.get("uecNationduty");
			if (null != uecNationduty) {
				PubUtil.fillBeanFromPo(uecNationduty, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		// 获取登录用户
		String currentName = "";
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户已经失效，请重新登录系统", "错误提示");
			return;
		} else {
			currentName = PlatformUtil.getCurrentUser().getScreenName();
		}
		if ("add".equals(opType)) {
			uecNationduty = UecNationduty.newInstance();
			PubUtil.fillPoFromBean(bean, uecNationduty);
			dateTransToString();
			uecNationduty.setStaffId(uecOutStaffInfo.getOutStaffId());
			uecNationduty.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			uecNationduty.setPkPsndoc(uecOutStaffInfo.getPkPsnMsg());
			uecNationduty.setCreator(currentName);
			uecNationduty.setCreationtime(DateUtil.getCurrenDate());

			//if (!isOutStaffEntryExt) {
				uecNationduty.addOnly();
			//}

			Events.postEvent("onOK", this.self, uecNationduty);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecNationduty);
			dateTransToString();
			uecNationduty.setModifier(currentName);
			uecNationduty.setModifiedtime(DateUtil.getCurrenDate());

			if (!isOutStaffEntryExt) {
				uecNationduty.updateOnly();
			}

			Events.postEvent("onOK", this.self, uecNationduty);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
		if ("approvalEdit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecNationduty);
			dateTransToString();
			uecNationduty.setModifier(currentName);
			uecNationduty.setModifiedtime(DateUtil.getCurrenDate());

			uecNationduty.updateOnly();

			Events.postEvent("onOK", this.self, uecNationduty);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getWbNationdutyEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (!"1001A1100000000006LV".equals(bean.getWorkname().getSelectedItem().getValue())) {
			 if (StrUtil.isNullOrEmpty(bean.getOccupation().getSelectedItem().getValue())) {
				return "请选择职业资格类别";
			} else if (StrUtil.isNullOrEmpty(bean.getWorklevel().getSelectedItem().getValue())) {
				return "请选择职业资格等级";
			}else if (StrUtil.isNullOrEmpty(bean.getWorkunitname().getValue())) {
				return "请填写评定机构";
			}else if (StrUtil.isNullOrEmpty(bean.getWorkdate().getValue())) {
				return "请填写获证日期";
			}
		}
		return null;
	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		// 获证日期
		if (!StrUtil.isNullOrEmpty(bean.getWorkdate().getValue())) {
			uecNationduty.setWorkdate(formatter.format(bean.getWorkdate().getValue()));
		}
		// 证书有效期
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef4().getValue())) {
			uecNationduty.setGlbdef4(formatter.format(bean.getGlbdef4().getValue()));
		}
	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 获证日期
		if (null != uecNationduty.getWorkdate()) {
			bean.getWorkdate().setValue(DateUtil.patternStrToDate(uecNationduty.getWorkdate()));
		}
		// 证书有效期
		if (null != uecNationduty.getGlbdef4()) {
			bean.getGlbdef4().setValue(DateUtil.patternStrToDate(uecNationduty.getGlbdef4()));
		}
	}

}
